﻿using CavinatorService.Business;
using CavinatorService.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using CavinatorService.Model.ID3;

namespace CavinatorService
{
    public class CavinatorService : ICavinatorService
    {
        #region Atributos

        //Variavel magica para armazenar as perguntas respondidas e respectivas respostas
        //ate o presente momento
        private static List<Answer> respondidas;

        //Variavel magica para armazenar o estado atual da ID3 (arvore)
        private static Node NoAtual;

        private ID3Business id3Business;
        private AnswerService answerService;
        private ItemService itemService;
        private QuestionService questionService;

        #endregion

        #region Construtor

        public CavinatorService()
        {
            id3Business = new ID3Business();
            answerService = new AnswerService();
            itemService = new ItemService();
            questionService = new QuestionService();
        }

        #endregion

        #region Métodos

        public Protocol GetNextState(Protocol protocolo)
        {
            if (protocolo == null)
            {
                respondidas = new List<Answer>();
            }
            else
            {
                if (protocolo.Answers != null && protocolo.Answers.Count > 0)
                {
                    respondidas.Add(protocolo.Answers[0]);
                }
            }

            return id3Business.ObterProximaPergunta(ref NoAtual, protocolo);
        }

        public List<Question> PerguntasAResponder()
        {
            var q = respondidas.Select(a => a.Question).ToList();
            return  questionService.PerguntasAResponder(q);
        }

        public List<Answer> PerguntasRespondidas()
        {
            return respondidas;
        }

        public void InserirNovoItem(Item registro)
        {
            itemService.Inserir(registro);
        }

        #endregion
    }
}
